/*
 * Jamus
 * Copyright (C) 2010 Jamus Team
 *
 * Jamus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jamus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jamus.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __FRONTEND_MPD_PLUGIN
#define __FRONTEND_MPD_PLUGIN

#include <boost/asio.hpp>
#include <boost/signals2.hpp>
#include <log4cxx/logger.h>

#include "../Plugin.h"
#include "Connection.h"

namespace frontend {
namespace mpd {

class Plugin : public frontend::Plugin, private boost::noncopyable
{
public:
	static const int DEFAULT_PORT;
	static const int DEFAULT_THREAD_POOL_SIZE;

	Plugin(backend::Plugin& backend, int port = DEFAULT_PORT, int threadPoolSize = DEFAULT_THREAD_POOL_SIZE);
	virtual ~Plugin();

	virtual void run();
	virtual void cancel();

	int getPlaylistVersion() const;
	int getTrackIdFromPlaylistIndex(int index) const;
	int getPlaylistIndexFromTrackId(int id) const;

	void onPlaylistChanged();
	
protected:
	static log4cxx::LoggerPtr logger;

	void onAccept(const boost::system::error_code&);

	const int threadPoolSize;

	boost::asio::io_service ios;
	boost::asio::ip::tcp::acceptor acceptor;

	// Next connection to be accepted
	ConnectionPtr newConnection;

	int playlistVersion;
	boost::signals2::connection playlistChangedSignalConnection;

	size_t playlistLength;
	int firstPlaylistEntryId;
};

} // namespace mpd
} // namespace frontend

#endif
